package com.wxit.datasource.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.wxit.datasource.mapper.UserMapper;
import com.wxit.datasource.pojo.User;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService {

    @Resource
    UserMapper userMapper;

    @DS("ds1")
    public User getByUserId1(Integer id) {
        return userMapper.getByID(id);
    }

    @DS("ds2")
    public User getByUserId2(Integer id) {
        return userMapper.getByID(id);
    }

    @DS("ds1")
    public void insertUserDs1(String name,int age) {
        userMapper.insertUserDs1(name,age);
    }

    @DS("ds2")
    public void insertUserDs2(String name,int age) {
        userMapper.insertUserDs2(name,age);
    }

    @DSTransactional
    public void insertUserAll(String name, int age){
        UserService userService = (UserService) AopContext.currentProxy();
        userService.insertUserDs1(name,age);
        userService.insertUserDs2(name,age);
//        int i =1/0;
    }
}
